package com.ruoyi.framework.web.service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.system.domain.SysErrorLog;
import com.ruoyi.system.service.SysErrorLogService;
import org.springframework.stereotype.Component;

import java.util.TimerTask;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Component
public class BusinessLogService {
	/**
	 * @param message 错误信息
	 * @param clazzName     类名
	 * @param methodName     方法名
	 */
	public void logError(String message, String clazzName, String methodName) {
		log(message, clazzName, methodName, SysErrorLog.ERROR);
	}

	public void logError(String message) {
		log(message, SysErrorLog.ERROR);
	}

	public void logWarning(String message) {
		log(message, SysErrorLog.WARNING);

	}

	private void log(String message, String status) {
		log(message, null, status);
	}

	private void log(String message, String clazzName, String status) {
		log(message, clazzName, null, status);
	}

	public void log(String message, String clazzName, String methodName, String status) {
		String username = SecurityUtils.getUsername();
		AsyncManager.me().execute(new TimerTask() {
			@Override
			public void run() {
				SpringUtils.getBean(SysErrorLogService.class).log(username, message, clazzName,
						methodName, status);
			}
		});
	}

	public static void main(String[] args) {
		System.out.println(new Object() {
		}.getClass().getEnclosingMethod().getName());
	}
}
